﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DBControl;
using System.Text;

namespace ProjectX
{
    public partial class Default : Errorbase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {   
                bindSource();
                if (Session["userNick"] != null)
                {
                    this.username.Text = Session["userNick"].ToString();
                    this.loginlink.Visible = false;
                    this.reglink.Visible = false;
                    this.logout.Visible = true;
                }
                //bindcate();
                //bindRepeater();
            }
        }


        private void bindSource()
        {
            StringBuilder sqlsb = new StringBuilder();
            string getBuy = "SELECT TOP 12 a.*,b.name AS catename FROM BuyersinfoR a LEFT JOIN cidcount b ON a.cateid = b.cid WHERE a.[Status]=0 ORDER BY Starttime DESC";
            string getCate = "SELECT TOP 9 * FROM CidCount WHERE  Cidlevel = 1 AND [Count] > 0 ORDER BY [Count] DESC";
            string getNews = "SELECT TOP 5 * FROM News ORDER BY newsTime DESC";
            string getaction = "SELECT * FROM actionView ORDER BY addtime DESC";
            sqlsb.Append(getBuy + ";" + getCate + ";" + getNews + ";" + getaction);
            SqlDbHelper sdh = new SqlDbHelper();
            DataSet ds = sdh.ExecuteDataSet(sqlsb.ToString());
            this.newRepeater.DataSource = ds.Tables[0];
            this.newRepeater.DataBind();
            this.catelist.DataSource = ds.Tables[1];
            this.catelist.DataBind();
            this.newslist.DataSource = ds.Tables[2];
            this.newslist.DataBind();
            this.actionlist.DataSource = ds.Tables[3];
            this.actionlist.DataBind();
        }

        private void bindacction()
        {
            string getaction = "SELECT * FROM actionView ORDER BY addtime DESC";
            SqlDbHelper sdh = new SqlDbHelper();
            this.actionlist.DataSource = sdh.ExecuteDataTable(getaction);
            this.actionlist.DataBind();
        }

        protected void actiontime_Tick(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(actiontime, this.GetType(), "cleartimer", "<script>cleartimer(0)</script>", false);
            bindacction();
        }

        protected void logout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Write("<script language='javascript'>window.parent.location.href='/'</script>");
        }
    }
}